package org.dbrd.textelements;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

@Deprecated
public class Tokenizer {

	private static final String SENTENCE_BREAK = "<sentence_break/>";
	
	private final static Map<String, Pattern> SYMBOL_PATTERNS;
	
	static {
		Map<String, Pattern> map = new HashMap<String, Pattern>();

		map.put("`", Pattern.compile("`"));
		map.put("^", Pattern.compile("\\^"));
		map.put("*", Pattern.compile("\\*"));
		map.put("=", Pattern.compile("="));
		map.put("+", Pattern.compile("\\+"));
		map.put("|", Pattern.compile("\\|"));
		map.put("\\\\", Pattern.compile("\\\\"));
		map.put("[", Pattern.compile("\\["));
		map.put("]", Pattern.compile("\\]"));
		map.put("}", Pattern.compile("\\}"));
		map.put("{", Pattern.compile("\\{"));
		map.put(",", Pattern.compile(","));
		map.put("!", Pattern.compile("!"));
		map.put("?", Pattern.compile("\\?"));
		map.put("#", Pattern.compile("#"));
		map.put("&", Pattern.compile("&"));
		map.put("(", Pattern.compile("\\("));
		map.put(")", Pattern.compile("\\)"));
		map.put("\"", Pattern.compile("\""));
		map.put(">", Pattern.compile("\\>"));
		map.put("<", Pattern.compile("\\<"));
		map.put("~", Pattern.compile("~"));
		map.put(";", Pattern.compile(";"));
		
		SYMBOL_PATTERNS = Collections.unmodifiableMap(map);
	}

	public String[] splitParagraphs(String text) {
		return text.split("\n+");
	}

	public String[] splitSentences(String text) {
		String[] words = text.split("\\s+");
		// end with '?', '!', '..', or '...' or '......++'
		Pattern breakPattern1 = Pattern.compile("([?!]+|[.][.]+)$");
		Pattern breakPattern2 = Pattern.compile("([.])$");

		for (int i = 0; i < words.length; i++) {
			String word = words[i];
			System.out.println(word);
			if (breakPattern1.matcher(word).find()) {
				// is a sentence break point;
				words[i] = word + " " + SENTENCE_BREAK;
				continue;
			}
			if (breakPattern2.matcher(word).find()) {
				if (!Dictionary.INSTANCE.isCommonAbbrevOrAcronym(word)) {
					// is a sentence break point
					words[i] = word + " " + SENTENCE_BREAK;
					continue;
				}
			}
		}
		StringBuilder builder = new StringBuilder();
		for (String word : words) {
			builder.append(word).append(" ");
		}
		String newText = builder.toString();
		// reduce multiple sentence breaks to one.
		newText = newText.replaceAll("(\\<sentence_break\\/\\> ?){2,20}", SENTENCE_BREAK + " ");
		// replace the sentence break at the end of the text
		newText = newText.replaceAll("(\\<(sentence|paragraph)_break\\/\\> *)+$", "");
		return newText.split("\\<sentence_break\\/\\>");
	}

	public String[] tokenize(String sentence) {
		Pattern pattern = Pattern.compile("^([A-Z][.])+$");
		
		sentence = " " + sentence.replaceAll("\\/", ":") + " ";
		for (Map.Entry<String, Pattern> entry: SYMBOL_PATTERNS.entrySet()) {
			sentence = entry.getValue().matcher(sentence).replaceAll(" " + entry.getKey() + " ");
		}
		String[] words = sentence.split("\\s+");
		for (int i = 0; i < words.length; i++) {
			String word = words[i];
			if (Dictionary.INSTANCE.isCommonAbbrevOrAcronym(word)) {
				continue;
			}
			if (pattern.matcher(word).find()) {
				continue;
			}
			
		}
		return null;
	}

	public static void main(String[] args) {
		// String[] s = new
		// Tokenizer().splitSentences("test\n\n\ntest test \f test.");
		// System.out.println(Arrays.toString(s));
		//
		// s = new Tokenizer().splitSentences("test\n\n\ntest test \f test .");
		// System.out.println(Arrays.toString(s));
		//
		// s = new Tokenizer().splitParagraphs("test\n\n\ntest test  test.");
		// System.out.println(Arrays.toString(s));

		String text = "California's officials were optimistic Monday about the full containment of the devastating wildfires that held 27,000 US residents displaced in southern California. All fires were expected to be surrounded by Tuesday, if not by Monday evening, said Andrea Tuttle, director of the California Department of Forestry and Fire Protection. Continued cooler weather associated with the rain, snow and freezing temperatures at night helped exhausted firefighters claimvictory over raging wildfires in the past days, Tuttle said, adding that \"the weather continues to be healthy for us.\"     About half of the 14,000 firefighters remained in the front tocombat forest fires in an area ranging from San Diego to San Bernardion in southern California till Monday. The Old Fire, the last of the wildfires that once threatened the populous ski resort of Lake Big Bear in the San Bernardino Mountains, was 83 percent contained. Fire engine crews sprayed smoky spots in some charred areas, and utility crews restrung lines to restore power. More than 27,000 people of about 80,000 evacuees remained displaced, most of whom are from the Lake Arrowhead area, where all roads west of state Highway 18 and state Highway 38 remained closed due to a threat of mudslides. Other areas closed to residents Monday include Crestline, Lake Gregory, Twin Peaks, Rimforest, Blue Jay, Cedar Glen, Skyforest, Running Springs, Arrowbear Lake, and Green Valley Lake.";

		String[] s = new Tokenizer().splitSentences(text);
		for (String ss : s) {
			System.out.println();
			System.out.println(ss);
		}
	}
}
